package com.padakeji.android.pworks.module.nav.view;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.view.View;
import com.padakeji.android.pworks.R;
import com.padakeji.android.pworks.app.PadaActivity;
import com.padakeji.android.pworks.module.nav.bean.NavItem;
import com.padakeji.android.pworks.widget.ViewPager;

/**
 * Created by AtanL on 2015/9/23.
 */
public abstract class ViewNavActivity extends PadaActivity {

    private NavItem[] navItems;
    private OnTabViewSelectedListener onTabViewSelectedListener;
    private boolean isPageSlidable = true;
    private Drawable navTabBackgroundDrawable;

    /**
     * 该方法用于产生导航的各个栏目。
     * 注意，该方法在UI主线程中运行。切勿在该方法中编写网络请求以及UI阻塞的任务。
     *
     * @return
     */
    protected abstract NavItem[] getNavItems();

    protected abstract OnTabViewSelectedListener getOnTabViewSelectedListener();


    /**
     * 该方法用于指定各个界面可否通过手势左右滑动进行切换。
     * @return
     */
    protected abstract boolean isPageSlidable();

    /**
     * 该方法用于覆盖TabLayout的背景。如果返回值是null，那么就不做处理。
     * @return
     */
    protected abstract Drawable getNavTabBackgroundDrawable();

    private ViewNavPagerAdapter navPagerAdapter;

    private ViewPager viewsPager;
    private TabLayout bottomTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pworks_activity_nav);

        navItems = getNavItems();
        onTabViewSelectedListener = getOnTabViewSelectedListener();
        isPageSlidable = isPageSlidable();
        navTabBackgroundDrawable = getNavTabBackgroundDrawable();

        navPagerAdapter = new ViewNavPagerAdapter(getSupportFragmentManager(), this, navItems);
        viewsPager = (ViewPager) findViewById(R.id.pworks_activity_nav_pager_views);
        bottomTab = (TabLayout) findViewById(R.id.pworks_activity_nav_tab_bottom);

        viewsPager.setScrollable(isPageSlidable);
        viewsPager.setAdapter(navPagerAdapter);
        viewsPager.setOffscreenPageLimit(navItems.length);
        if (navTabBackgroundDrawable != null) {
            bottomTab.setBackgroundDrawable(navTabBackgroundDrawable);
        }
        bottomTab.setupWithViewPager(viewsPager);
        bottomTab.setTabsFromPagerAdapter(navPagerAdapter);

        int i, count = bottomTab.getTabCount();
        for (i = 0; i < count; i++) {
            TabLayout.Tab tab = bottomTab.getTabAt(i);
            View tabView = navPagerAdapter.getPageTabView(i);
            if (tabView != null) {
                tab.setCustomView(navPagerAdapter.getPageTabView(i));
            }
        }

        bottomTab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewsPager.setCurrentItem(tab.getPosition(), false);
                onTabViewSelectedListener.onTabViewSelected(tab.getPosition(), true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                onTabViewSelectedListener.onTabViewSelected(tab.getPosition(), false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                onTabViewSelectedListener.onTabViewSelected(tab.getPosition(), true);
            }
        });

        bottomTab.getTabAt(viewsPager.getCurrentItem()).select();

    }

}
